package com.golddaymicromall.activity.salemanager;

import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.golddaymicromall.activity.R;
import com.golddaymicromall.adapter.SalePurchaseAdapter;
import com.golddaymicromall.base.BaseActivity;
import com.golddaymicromall.fragment.Sale_FirstPurchaseFragment;
import com.golddaymicromall.fragment.Sale_SecondPurchaseFragment;
import com.golddaymicromall.fragment.Sale_StoreUpgradeFragment;
import com.golddaymicromall.util.SPUtils;

import java.util.ArrayList;
import java.util.List;

public class SalePurchaseActivity extends BaseActivity implements ViewPager.OnPageChangeListener {

    private FragmentManager fm;
    private ViewPager mVp;
    private SalePurchaseAdapter Adapter;
    private Sale_FirstPurchaseFragment mSale_FirstPurchaseFragment;
    private Sale_SecondPurchaseFragment mSale_SecondPurchaseFragment;
    private Sale_StoreUpgradeFragment mSale_StoreUpgradeFragment;
    List<Fragment> fragmentList = new ArrayList<Fragment>();
    private LinearLayout mFirstPurchase;
    private LinearLayout mSecondPurchase;
    private LinearLayout mStoreUpgrade;
    private TextView mFirstLine;
    private TextView mSecondLine;
    private TextView mUpgradeLine;
    private LinearLayout mSalePurchaseBack;

    String turnState;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sale_purchase);
        initView();
        initList();
    }

    private void initView() {

        turnState = SPUtils.get(getApplicationContext(), "turndate", "").toString();
        mFirstPurchase = (LinearLayout) findViewById(R.id.fitst_purchase);
        mSecondPurchase = (LinearLayout) findViewById(R.id.second_purchase);
        mStoreUpgrade = (LinearLayout) findViewById(R.id.store_upgrade);
        mFirstLine = (TextView) findViewById(R.id.fitst_line);
        mSecondLine = (TextView) findViewById(R.id.second_line);
        mUpgradeLine = (TextView) findViewById(R.id.upgrade_line);
        mVp = (ViewPager) findViewById(R.id.PurchaseActivity_VP1);
        mSalePurchaseBack = (LinearLayout) findViewById(R.id.sale_purchase_back);
        mVp.setOnPageChangeListener(this);
        if ("".equals(turnState) || "0".equals(SPUtils.get(getApplicationContext(), "firstFlag", "").toString())) {

            mFirstPurchase.setOnClickListener(new mVPListener(0));
            mFirstLine.setVisibility(View.VISIBLE);
            mFirstLine.setBackgroundColor(getResources().getColor(R.color.pink));
            mSecondPurchase.setVisibility(View.GONE);
            mSecondLine.setVisibility(View.GONE);
            mUpgradeLine.setVisibility(View.GONE);
            mStoreUpgrade.setVisibility(View.GONE);
        } else if (!"".equals(turnState) && Integer.parseInt(SPUtils.get(getApplicationContext(), "firstFlag", "").toString()) >= 1) {
            mSecondPurchase.setOnClickListener(new mVPListener(0));
            mFirstLine.setVisibility(View.GONE);
            mFirstPurchase.setVisibility(View.GONE);
            mSecondLine.setVisibility(View.VISIBLE);
            mSecondLine.setBackgroundColor(getResources().getColor(R.color.pink));
            if ("1".equals(SPUtils.get(getApplicationContext(), "shop_Grade", "").toString())) {
                mStoreUpgrade.setOnClickListener(new mVPListener(1));
                mStoreUpgrade.setVisibility(View.VISIBLE);
            } else {
                mUpgradeLine.setVisibility(View.GONE);
                mStoreUpgrade.setVisibility(View.GONE);
            }
        }
        mSalePurchaseBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initList() {
        fm = getSupportFragmentManager();
        if ("".equals(turnState) || "0".equals(SPUtils.get(getApplicationContext(), "firstFlag", "").toString())) {
            fragmentList.add(new Sale_FirstPurchaseFragment());
        } else if (!"".equals(turnState) && Integer.parseInt(SPUtils.get(getApplicationContext(), "firstFlag", "").toString()) >= 1) {
            fragmentList.add(new Sale_SecondPurchaseFragment());
            if ("1".equals(SPUtils.get(getApplicationContext(), "shop_Grade", "").toString())) {
                fragmentList.add(new Sale_StoreUpgradeFragment());
            }
        }
        Adapter = new SalePurchaseAdapter(fm, fragmentList);
        mVp.setAdapter(Adapter);
    }

    class mVPListener implements View.OnClickListener {
        private int index = 0;

        public mVPListener(int i) {
            index = i;
        }

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            mVp.setCurrentItem(index);
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        switch (position) {
            case 0:
                mSecondLine.setBackgroundColor(getResources().getColor(R.color.pink));
                mFirstLine.setVisibility(View.INVISIBLE);
                mSecondLine.setVisibility(View.VISIBLE);
                mUpgradeLine.setVisibility(View.INVISIBLE);
                break;
            case 1:
                mUpgradeLine.setBackgroundColor(getResources().getColor(R.color.pink));
                mFirstLine.setVisibility(View.INVISIBLE);
                mSecondLine.setVisibility(View.INVISIBLE);
                mUpgradeLine.setVisibility(View.VISIBLE);
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    //清理顶部导航textview文字颜色方法
    private void clearTvColor() {
        mUpgradeLine.setTextColor(Color.GRAY);
        mFirstLine.setTextColor(Color.GRAY);
        mSecondLine.setTextColor(Color.GRAY);

    }
}
